<!DOCTYPE html>
<html lang="en">

<head>
    <title>PHP TdLib</title>
    <!-- Meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="favicon.ico">
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800'
          rel='stylesheet' type='text/css'>
    <!-- FontAwesome JS -->
    <script defer src="https://use.fontawesome.com/releases/v5.1.1/js/all.js"
            integrity="sha384-BtvRZcyfv4r0x/phJt9Y9HhnN5ur1Z+kZbKVgzVBAlQZX4jvAuImlIz+bG7TS00a"
            crossorigin="anonymous"></script>
    <!-- Global CSS -->
    <link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css">
    <!-- Plugins CSS -->
    <link rel="stylesheet" href="assets/plugins/prism/prism.css">
    <link rel="stylesheet" href="assets/plugins/elegant_font/css/style.css">

    <!-- Theme CSS -->
    <link id="theme-style" rel="stylesheet" href="assets/css/styles.css">

</head>

<body class="body-green">
<div class="page-wrapper">
    <!-- ******Header****** -->
    <header id="header" class="header">
        <div class="container">
                <span style="font-size: 3em; position:absolute; top:20px; right:40px">
                    <a href="https://github.com/yaroslavche/phptdlib" target="_blank"><i class="fab fa-github"></i></a>
                </span>
            <div class="branding">
                <h1 class="logo">
                    <a href="index.html">
                        <span aria-hidden="true" class="fa fa-paper-plane icon"></span>
                        <span class="text-highlight">PHP</span><span class="text-bold">TdLib</span>
                    </a>
                </h1>
            </div>
            <!--//branding-->
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="index.html">Home</a></li>
                <li class="breadcrumb-item active">Documentation</li>
            </ol>
        </div>
        <!--//container-->
    </header>
    <!--//header-->
    <div class="doc-wrapper">
        <div class="container">
            <div id="doc-header" class="doc-header text-center">
                <h1 class="doc-title"><i class="icon icon_cogs"></i> Documentation</h1>
                <div class="meta"><i class="far fa-clock"></i> Last updated: February 19, 2020</div>
            </div>
            <!--//doc-header-->
            <div class="doc-body row">
                <div class="doc-content col-md-9 col-12 order-1">
                    <div class="content-inner">
                        <section id="example-section" class="doc-section">
                            <h2 class="section-title">How it works in few words</h2>
                            <div id="tdlib" class="section-block">
                                <div class="code-block">
                                    <h6>TDLib</h6>
                                    For working with Telegram database, you need to have a configured and authorized
                                    client.
                                    So, you should create a new client instance:
                                    <pre><code class="language-php">
$client = td_json_client_create();</code></pre>
                                    Then you can send requests to Telegram database. Both types: synchronous and
                                    asynchronous.<br>
                                    But most of requests to database should be async. Then you should use <b>send</b>
                                    and <b>receive</b>:
                                    <pre><code class="language-php">
td_json_client_send($client, json_encode(['@type' =&gt; 'someType']));
$result = td_json_client_receive($client, 10);</code></pre>
                                    <b>someType</b> here is a type (object/method) from <a
                                        href="https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1_function.html">tdlib
                                    Function Class Reference</a>.<br>
                                    For instance: <a
                                        href="https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1get_authorization_state.html">getAuthorizationState</a>.<br><br>
                                    For sync request to database you should use
                                    <pre><code class="language-php">
$result = td_json_client_execute($client, json_encode(['@type' =&gt; 'someType']));</code></pre>
                                    As you can see, you have a <b>$result</b> in that function call (and <b>td_json_client_send</b>
                                    haven't).
                                </div>
                                And then, when you have done your business, you should destroy a client.
                                <pre><code class="language-php">
td_json_client_destroy($client);</code></pre>
                                Same things can be done in object-oriented way:
                                <pre><code class="language-php">
$client = new \TDLib\JsonClient();
$client->setDefaultTimeout(10);
$result = $client->execute($stringQuery);
$client->send($stringQuery);
$response = $client->receive($floatTimeout);
$response = $client->query($stringQuery, $floatTimeout);
$responses = $client->getReceivedResponses();
$client->destroy();</code></pre>
                                Where <b>$stringQuery</b> is already formatted json with "@type" and arguments.</b>
                                Also exists two helper methods: <b>$client->query</b> and <b>$client->getReceivedResponses</b>.
                                Query is just send request with <b>@extra</b> and then wait that field in responses.
                                Since this is for asynchronous requests, before you get an answer to the request, you
                                can get others. And you can get all the responses to the last request with
                                getReceivedResponses

                            </div>
                            <!--//section-block-->
                            <div id="tdlib-client" class="section-block">
                                <div class="code-block">
                                    <h6>Preparing a client</h6>
                                    Before you can search chats, send messages and receive them, you should have
                                    authorized client.
                                    You can check your client state with <a
                                        href="https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1get_authorization_state.html">getAuthorizationState</a>
                                    request.
                                    Only if response will be ok, then you allowed to do what you want. Otherwise in
                                    response will contain a message what client wants to be configured or set<br>
                                    So, firstly after client was created, need send few request:<br>
                                    <ul>
                                        <li>
                                            <a href="https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1set_tdlib_parameters.html">setTdlibParameters</a>
                                        </li>
                                        <li>
                                            <a href="https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1set_database_encryption_key.html">setDatabaseEncryptionKey</a>
                                        </li>
                                        <li>
                                            <a href="https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1set_authentication_phone_number.html">setAuthenticationPhoneNumber</a>
                                        </li>
                                        <li>and after receiving a SMS with code: <a
                                                href="https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1check_authentication_code.html">checkAuthenticationCode</a>
                                        </li>
                                    </ul>
                                    Better explanation you can find <a
                                        href="https://core.telegram.org/tdlib/getting-started">here</a>
                                    Almost complete example is below. But please remember, that this is async requests,
                                    so it should be handled also async
                                    <!--//code-block-->
                                </div>
                            </div>
                            <!--//section-block-->
                            <div id="example-client" class="section-block">
                                <div class="code-block">
                                    <h6>TDLib\JsonClient</h6>
                                    <pre><code class="language-php">&lt;?php

Error_Reporting(E_ALL);
ini_set('display_errors', 1);

$api_id = 11111; // must be integer
$api_hash = 'abcdef1234567890abcdef1234567890';
$phone_number = '+380991234567';

try {
    TDApi\LogConfiguration::setLogVerbosityLevel(\TDApi\LogConfiguration::LVL_ERROR);

    $client = new TDLib\JsonClient();
    $client->getAuthorizationState();

    $tdlibParams = new TDApi\TDLibParameters();
    $tdlibParams
        ->setParameter(TDApi\TDLibParameters::USE_TEST_DC, true)
        ->setParameter(TDApi\TDLibParameters::DATABASE_DIRECTORY, '/var/tmp/tdlib')
        ->setParameter(TDApi\TDLibParameters::FILES_DIRECTORY, '/var/tmp/tdlib')
        ->setParameter(TDApi\TDLibParameters::USE_FILE_DATABASE, false)
        ->setParameter(TDApi\TDLibParameters::USE_CHAT_INFO_DATABASE, false)
        ->setParameter(TDApi\TDLibParameters::USE_MESSAGE_DATABASE, false)
        ->setParameter(TDApi\TDLibParameters::USE_SECRET_CHATS, false)
        ->setParameter(TDApi\TDLibParameters::API_ID, $api_id)
        ->setParameter(TDApi\TDLibParameters::API_HASH, $api_hash)
        ->setParameter(TDApi\TDLibParameters::SYSTEM_LANGUAGE_CODE, 'en')
        ->setParameter(TDApi\TDLibParameters::DEVICE_MODEL, php_uname('s'))
        ->setParameter(TDApi\TDLibParameters::SYSTEM_VERSION, php_uname('v'))
        ->setParameter(TDApi\TDLibParameters::APPLICATION_VERSION, '0.0.10')
        ->setParameter(TDApi\TDLibParameters::ENABLE_STORAGE_OPTIMIZER, true)
        ->setParameter(TDApi\TDLibParameters::IGNORE_FILE_NAMES, false);
    $result = $client->setTdlibParameters($tdlibParams);
    $result = $client->setDatabaseEncryptionKey(); // checkDatabaseEncryptionKey(key) or DESTROY
    $result = $client->setAuthenticationPhoneNumber($phone_number, 3); // wait response 3 seconds. default - 1.
    // UNCOMMENT NEXT and COMMENT PREVIOUS LINES WHEN RECEIVE SMS. Set your data
    // $result = $client->query(json_encode(['@type' => 'checkAuthenticationCode', 'code' => 'xxxxx', 'first_name' => 'dummy', 'last_name' => 'dummy']), 10);
    $result = $client->getAuthorizationState();
    $result = $client->query(json_encode(['@type' => 'searchPublicChat', 'username' => 'telegram']), 10);
    var_dump($result);
    $allNotifications = $client->getReceivedResponses();
    var_dump($allNotifications);
} catch (\Exception $exception) {
    echo sprintf('something goes wrong: %s', $exception->getMessage());
}</code></pre>
                                </div>
                                <!--//code-block-->
                            </div>
                            <!--//section-block-->
                            <div id="example-send-message" class="section-block">
                                <div class="code-block">
                                    <h6>sendMessage example</h6>
                                    <pre><code class="language-php">&lt;php

$chat_id=-1001190992756;
$message_text = 'hello';
$print_timeout = 1;

$client->query(json_encode([
    '@type'=>'openChat',
    'chat_id'=>$chat_id
]));

$client->query(json_encode([
    '@type'   => 'sendChatAction',
    'chat_id' => $chat_id,
    'action'  => [
        '@type' => 'chatActionTyping'
    ]
]));

sleep(1);

$client->query(json_encode([
    '@type'   => 'sendChatAction',
    'chat_id' => $chat_id,
    'action'  => [
        '@type' => 'chatActionCancel'
    ]
]));


$res=$client->query(json_encode([
    '@type'=>'sendMessage',
    'chat_id'=>$chat_id,
    'reply_to_message_id'=>0,
    'disable_notification'=>false,
    'from_background'=>false,
    'input_message_content'=>[
        '@type'=>'inputMessageText',
        'text'=>[
            '@type'=>'formattedText',
            'text'=>$message_text,
            'entities'=>[]
        ],
        'disable_web_page_preview'=>false,
        'clear_draft'=>false
    ]
]));

$client->query(json_encode([
    '@type'   => 'closeChat',
    'chat_id' => $chat_id
]));

if(!empty($res['last_message']['sendingState']) && $res['last_message']['sendingState']['@type']==='messageSendingStateFailed') {
    throw new Exception('Message sending failed');
}
</code></pre>
                                </div>
                                <!--//code-block-->
                            </div>
                            <!--//section-block-->
                            <div id="example-add-proxy" class="section-block">
                                <div class="code-block">
                                    <h6><a href="https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1add_proxy.html">addProxy</a> example</h6>
                                    <pre><code class="language-php">&lt;php

$proxy = [
    '@type' => 'proxyTypeSocks5',
    'username' => $proxyUsername,
    'password' => $proxyPassword
];

$client->query(json_encode([
    '@type' => 'addProxy',
    'server' => $proxyHost,
    'port' => (int)$proxyPort,
    'enable' => true,
    'type' => $proxy
]), 10);
</code></pre>
                                </div>
                                <!--//code-block-->
                            </div>
                            <!--//section-block-->
                        </section>
                        <!--//doc-section-->
                    </div>
                    <!--//content-inner-->
                </div>
                <!--//doc-content-->
                <div class="doc-sidebar col-md-3 col-12 order-0 d-none d-md-flex">
                    <div id="doc-nav" class="doc-nav">
                        <nav id="doc-menu" class="nav doc-menu flex-column sticky">
                            <a class="nav-link scrollto" href="#example-section">Documentation</a>
                            <nav class="doc-sub-menu nav flex-column">
                                <a class="nav-link scrollto" href="#tdlib">How it works</a>
                                <a class="nav-link scrollto" href="#tdlib-client">Preparing a client</a>
                                <a class="nav-link scrollto" href="#example-client">TDLib\JsonClient example</a>
                                <a class="nav-link scrollto" href="#example-send-message">sendMessage example</a>
                                <a class="nav-link scrollto" href="#example-add-proxy">addProxy example</a>
                            </nav>
                            <!--//nav-->
                        </nav>
                        <!--//doc-menu-->
                    </div>
                </div>
                <!--//doc-sidebar-->
            </div>
            <!--//doc-body-->
        </div>
        <!--//container-->
    </div>
    <!--//doc-wrapper-->

</div>
<!--//page-wrapper-->

<footer id="footer" class="footer text-center">
    <div class="container">
        <div class="row">
            <div class="col-md-4">
                <!-- GITHUB BUTTON JS -->
                <script async defer src="https://buttons.github.io/buttons.js"></script>
                <a class="github-button" href="https://github.com/yaroslavche/phptdlib" data-size="small"
                   aria-label="Star yaroslavche/phptdlib on GitHub">Star</a>
            </div>
            <div class="col-md-4">
                <!--/* This template is released under the Creative Commons Attribution 3.0 License. Please keep the attribution link below when using for your own project. Thank you for your support. :) If you'd like to use the template without the attribution, you can buy the commercial license via our website: themes.3rdwavemedia.com */-->
                <small class="copyright">Designed with <i class="fas fa-heart"></i> by <a
                        href="https://themes.3rdwavemedia.com/" target="_blank">Xiaoying Riley</a> for developers
                </small>
            </div>
            <div class="col-md-4" style="overflow: hidden;height: 50px;margin-top: -15px;">
                <!-- JetBrains logo -->
                <a href="https://www.jetbrains.com/?from=phptdlib" style="margin-top: -24px; position: absolute;">
                    <svg width="100px" height="100px" id="Layer_1" data-name="Layer 1"
                         xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120.21259 130.10278">
                        <title>JetBrains</title>
                        <g>
                            <path d="M120.21249,68.6a4.9968,4.9968,0,0,0-1.4-3.5L55.20349,2.68736a7.86372,7.86372,0,0,0-3.36731-2.261c-.03467-.012-.06812-.02612-.103-.03766-.19336-.06323-.3913-.11511-.591-.16327-.06689-.0163-.13177-.0368-.1994-.05133-.18744-.04-.37909-.06738-.57117-.09363-.0788-.0108-.15527-.0274-.23492-.03589A7.839,7.839,0,0,0,49.31249,0a7.73961,7.73961,0,0,0-1.21088.10413c-.0235.00391-.04694.00671-.07037.0108a7.62573,7.62573,0,0,0-3.092,1.24969c-.07343.04773-.155.08575-.22668.13538L4.91249,28.3c-.08221.08221-.15106.10632-.17773.16437L4.68467,28.5H4.61249A11.11647,11.11647,0,0,0,.17127,39.45685l.00854.04449c.05176.28589.11011.56958.18372.84973.03052.119.06964.235.104.35284.054.181.10278.3634.16571.5412A11.15109,11.15109,0,0,0,5.31249,47.1a8.8203,8.8203,0,0,0,2,.9c.4.2,45.4,18.8,45.4,18.8a4.291,4.291,0,0,0,4.4-7.3c-.06525,0-16.839-13.21332-28.69928-22.52606l21.105-19.03113,57.91815,49.58282L28.61249,110.7a9.82332,9.82332,0,0,0-4.7,4.1,10.06619,10.06619,0,0,0,3.44961,13.81148q.07481.04491.15039.08852a10.28687,10.28687,0,0,0,10.7-.2c.2-.2.5-.3.7-.5l78-54.8a18.32022,18.32022,0,0,0,1.58611-1.2663A4.74576,4.74576,0,0,0,120.21249,68.6Z"
                                  fill="#fff"/>
                            <g id="_Group_" data-name=" Group ">
                                <rect id="_Path_" data-name=" Path " x="34.61249" y="37.4" width="51" height="51"/>
                                <rect id="_Path_2" data-name=" Path 2" x="39.0125" y="78.8" width="19.1" height="3.2"
                                      fill="#fff"/>
                                <g id="_Group_2" data-name=" Group 2">
                                    <path id="_Path_3" data-name=" Path 3"
                                          d="M38.81249,50.8l1.5-1.4a1.70272,1.70272,0,0,0,1.3.8q.9,0,.9-1.2V43.7h2.3V49a2.79543,2.79543,0,0,1-3.1,3.1A3.026,3.026,0,0,1,38.81249,50.8Z"
                                          fill="#fff"/>
                                    <path id="_Path_4" data-name=" Path 4"
                                          d="M45.31249,43.8h6.7v1.9h-4.4V47h4v1.8h-4v1.3h4.5v2h-6.7Z" fill="#fff"/>
                                    <path id="_Path_5" data-name=" Path 5"
                                          d="M55.01249,45.8h-2.5v-2h7.3v2h-2.5v6.3h-2.3Z" fill="#fff"/>
                                    <path id="_Compound_Path_" data-name=" Compound Path "
                                          d="M39.0125,54h4.3a3.70232,3.70232,0,0,1,2.3.7,1.97823,1.97823,0,0,1,.5,1.4h0a1.95538,1.95538,0,0,1-1.3,1.9,1.94762,1.94762,0,0,1,1.6,2h0c0,1.4-1.2,2.3-3.1,2.3h-4.3Zm4.8,2.6c0-.5-.4-.7-1-.7h-1.5v1.5h1.4C43.4125,57.3,43.81249,57.1,43.81249,56.6Zm-.8,2.4h-1.8v1.5h1.8c.7,0,1.1-.3,1.1-.8h0C44.11249,59.2,43.7125,59,43.0125,59Z"
                                          fill="#fff"/>
                                    <path id="_Compound_Path_2" data-name=" Compound Path 2"
                                          d="M46.81249,54h3.9a3.51462,3.51462,0,0,1,2.7.9,2.48948,2.48948,0,0,1,.7,1.9h0a2.76052,2.76052,0,0,1-1.7,2.6l2,2.9h-2.6l-1.7-2.5h-1v2.5h-2.3Zm3.8,4c.8,0,1.2-.4,1.2-1h0c0-.7-.5-1-1.2-1h-1.5v2Z"
                                          fill="#fff"/>
                                    <path id="_Compound_Path_3" data-name=" Compound Path 3"
                                          d="M56.81249,54h2.2l3.5,8.4h-2.5l-.6-1.5h-3.2l-.6,1.5h-2.4Zm2,5-.9-2.3-.9,2.3Z"
                                          fill="#fff"/>
                                    <path id="_Path_6" data-name=" Path 6" d="M62.81249,54h2.3v8.3h-2.3Z" fill="#fff"/>
                                    <path id="_Path_7" data-name=" Path 7"
                                          d="M65.7125,54h2.1l3.4,4.4V54h2.3v8.3h-2l-3.5-4.5v4.6h-2.3Z" fill="#fff"/>
                                    <path id="_Path_8" data-name=" Path 8"
                                          d="M73.7125,61.1l1.3-1.5a3.94222,3.94222,0,0,0,2.7,1c.6,0,1-.2,1-.6h0c0-.4-.3-.5-1.4-.8-1.8-.4-3.1-.9-3.1-2.6h0c0-1.5,1.2-2.7,3.2-2.7a5.33073,5.33073,0,0,1,3.4,1.1l-1.2,1.6a4.55349,4.55349,0,0,0-2.3-.8c-.6,0-.8.2-.8.5h0c0,.4.3.5,1.4.8,1.9.4,3.1,1,3.1,2.6h0c0,1.7-1.3,2.7-3.4,2.7A5.29335,5.29335,0,0,1,73.7125,61.1Z"
                                          fill="#fff"/>
                                </g>
                            </g>
                        </g>
                    </svg>
                </a>
            </div>
        </div>
    </div>
    <!--//container-->
</footer>
<!--//footer-->


<!-- Main Javascript -->
<script type="text/javascript" src="assets/plugins/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="assets/plugins/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="assets/plugins/prism/prism.js"></script>
<script type="text/javascript" src="assets/plugins/jquery-scrollTo/jquery.scrollTo.min.js"></script>
<script type="text/javascript" src="assets/plugins/stickyfill/dist/stickyfill.min.js"></script>
<script type="text/javascript" src="assets/js/main.js"></script>

</body>

</html>
